home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / Fl_Pixmap.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-16  |  6.2 KB  |  199 lines

  1. //
  2. // "$Id: Fl_Pixmap.cxx,v 1.9 1999/02/16 15:10:53 mike Exp $"
  3. //
  4. // Pixmap drawing code for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // Draws X pixmap data, keeping it stashed in a server pixmap so it
  27. // redraws fast.
  28.  
  29. // See fl_draw_pixmap.C for code used to get the actual data into pixmap.
  30. // Implemented without using the xpm library (which I can't use because
  31. // it interferes with the color cube used by fl_draw_image).
  32.  
  33. #include <FL/Fl.H>
  34. #include <FL/fl_draw.H>
  35. #include <FL/x.H>
  36. #include <FL/Fl_Widget.H>
  37. #include <FL/Fl_Menu_Item.H>
  38. #include <FL/Fl_Pixmap.H>
  39.  
  40. #include <stdio.h>
  41.  
  42. extern uchar **fl_mask_bitmap; // used by fl_draw_pixmap.C to store mask
  43. void fl_restore_clip(); // in fl_rect.C
  44.  
  45. void Fl_Pixmap::draw(int XP, int YP, int WP, int HP, int cx, int cy) {
  46.   // ignore empty or bad pixmap data:
  47.   if (w<0) {
  48.     fl_measure_pixmap(data, w, h);
  49.     if (WP==-1) { WP = w; HP = h; }
  50.   }
  51.   if (!w) return;
  52.   // account for current clip region (faster on Irix):
  53.   int X,Y,W,H; fl_clip_box(XP,YP,WP,HP,X,Y,W,H);
  54.   cx += X-XP; cy += Y-YP;
  55.   // clip the box down to the size of image, quit if empty:
  56.   if (cx < 0) {W += cx; X -= cx; cx = 0;}
  57.   if (cx+W > w) W = w-cx;
  58.   if (W <= 0) return;
  59.   if (cy < 0) {H += cy; Y -= cy; cy = 0;}
  60.   if (cy+H > h) H = h-cy;
  61.   if (H <= 0) return;
  62.   if (!id) {
  63.     id = (ulong)fl_create_offscreen(w, h);
  64.     fl_begin_offscreen((Fl_Offscreen)id);
  65.     uchar *bitmap = 0;
  66.     fl_mask_bitmap = &bitmap;
  67.     fl_draw_pixmap(data, 0, 0, FL_BLACK);
  68.     fl_mask_bitmap = 0;
  69.     if (bitmap) {
  70. #ifdef WIN32 // Matt: mask done
  71.       // this won't work ehen the user changes display mode during run or
  72.       // has two screens with differnet depths
  73.       static uchar hiNibble[16] =
  74.       { 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
  75.     0x10, 0x90, 0x50, 0xd0, 0x20, 0xb0, 0x70, 0xf0 };
  76.       static uchar loNibble[16] =
  77.       { 0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e,
  78.     0x01, 0x09, 0x05, 0x0d, 0x02, 0x0b, 0x07, 0x0f };
  79.       int np  = GetDeviceCaps(fl_gc, PLANES);    //: was always one on sample machines
  80.       int bpp = GetDeviceCaps(fl_gc, BITSPIXEL);//: 1,4,8,16,24,32 and more odd stuff?
  81.       int Bpr = (bpp*w+7)/8;            //: bytes per row
  82.       int pad = Bpr&1, w1 = (w+7)/8, shr = ((w-1)&7)+1;
  83.       if (bpp==4) shr = (shr+1)/2;
  84.       uchar *newarray = new uchar[(Bpr+pad)*h], *dst = newarray, *src = bitmap;
  85.       for (int i=0; i<h; i++) {
  86.     //: this is slooow, but we do it only once per pixmap
  87.     for (int j=w1; j>0; j--) {
  88.       uchar b = *src++;
  89.       if (bpp==1) {
  90.         *dst++ = ( hiNibble[b&15] ) | ( loNibble[(b>>4)&15] );
  91.       } else if (bpp==4) {
  92.         for (int k=(j==1)?shr:4; k>0; k--) {
  93.           *dst++ = "\377\360\017\000"[b&3];
  94.           b = b >> 2;
  95.         }
  96.       } else {
  97.         for (int k=(j==1)?shr:8; k>0; k--) {
  98.           if (b&1) {
  99.         *dst++=0;
  100.         if (bpp>8) *dst++=0;
  101.         if (bpp>16) *dst++=0;
  102.         if (bpp>24) *dst++=0;
  103.           } else {
  104.         *dst++=0xff;
  105.         if (bpp>8) *dst++=0xff;
  106.         if (bpp>16) *dst++=0xff;
  107.         if (bpp>24) *dst++=0xff;
  108.           }
  109.           b = b >> 1;
  110.         }
  111.       }
  112.     }
  113.     dst += pad;
  114.       }
  115.       mask = (ulong)CreateBitmap(w, h, np, bpp, newarray);
  116.       delete[] newarray;
  117. #else
  118.       mask = XCreateBitmapFromData(fl_display, fl_window,
  119.                 (const char*)bitmap, (w+7)&-8, h);
  120. #endif
  121.       delete[] bitmap;
  122.     }
  123.     fl_end_offscreen();
  124.   }
  125. #ifdef WIN32
  126.   if (mask) {
  127.     HDC new_gc = CreateCompatibleDC(fl_gc);
  128.     SelectObject(new_gc, (void*)mask);
  129.     BitBlt(fl_gc, X, Y, W, H, new_gc, cx, cy, SRCAND);
  130.     SelectObject(new_gc, (void*)id);
  131.     BitBlt(fl_gc, X, Y, W, H, new_gc, cx, cy, SRCPAINT);
  132.     DeleteDC(new_gc);
  133.   } else {
  134.     fl_copy_offscreen(X, Y, W, H, (Fl_Offscreen)id, cx, cy);
  135.   }
  136. #else
  137.   if (mask) {
  138.     // I can't figure out how to combine a mask with existing region,
  139.     // so cut the image down to a clipped rectangle:
  140.     int nx, ny; fl_clip_box(X,Y,W,H,nx,ny,W,H);
  141.     cx += nx-X; X = nx;
  142.     cy += ny-Y; Y = ny;
  143.     // make X use the bitmap as a mask:
  144.     XSetClipMask(fl_display, fl_gc, mask);
  145.     int ox = X-cx; if (ox < 0) ox += w;
  146.     int oy = Y-cy; if (oy < 0) oy += h;
  147.     XSetClipOrigin(fl_display, fl_gc, X-cx, Y-cy);
  148.   }
  149.   fl_copy_offscreen(X, Y, W, H, (Fl_Offscreen)id, cx, cy);
  150.   if (mask) {
  151.     // put the old clip region back
  152.     XSetClipOrigin(fl_display, fl_gc, 0, 0);
  153.     fl_restore_clip();
  154.   }
  155. #endif
  156. }
  157.  
  158. Fl_Pixmap::~Fl_Pixmap() {
  159.   if (id) fl_delete_offscreen((Fl_Offscreen)id);
  160.   if (mask) fl_delete_offscreen((Fl_Offscreen)mask);
  161. }
  162.  
  163. static void pixmap_labeltype(
  164.                                                          const Fl_Label* o, int x, int y, int w, int h, Fl_Align a)
  165. {
  166.   Fl_Pixmap* b = (Fl_Pixmap*)(o->value);
  167.   if (b->w<0) fl_measure_pixmap(b->data, b->w, b->h);
  168.   int cx;
  169.   if (a & FL_ALIGN_LEFT) cx = 0;
  170.   else if (a & FL_ALIGN_RIGHT) cx = b->w-w;
  171.   else cx = (b->w-w)/2;
  172.   int cy;
  173.   if (a & FL_ALIGN_TOP) cy = 0;
  174.   else if (a & FL_ALIGN_BOTTOM) cy = b->h-h;
  175.   else cy = (b->h-h)/2;
  176.   b->draw(x,y,w,h,cx,cy);
  177. }
  178.  
  179. static void pixmap_measure(const Fl_Label* o, int& w, int& h) {
  180.   Fl_Pixmap* b = (Fl_Pixmap*)(o->value);
  181.   if (b->w<0) fl_measure_pixmap(b->data, b->w, b->h);
  182.   w = b->w;
  183.   h = b->h;
  184. }
  185.  
  186. void Fl_Pixmap::label(Fl_Widget* o) {
  187.   Fl::set_labeltype(_FL_PIXMAP_LABEL, pixmap_labeltype, pixmap_measure);
  188.   o->label(_FL_PIXMAP_LABEL, (const char*)this);
  189. }
  190.  
  191. void Fl_Pixmap::label(Fl_Menu_Item* o) {
  192.   Fl::set_labeltype(_FL_PIXMAP_LABEL, pixmap_labeltype, pixmap_measure);
  193.   o->label(_FL_PIXMAP_LABEL, (const char*)this);
  194. }
  195.  
  196. //
  197. // End of "$Id: Fl_Pixmap.cxx,v 1.9 1999/02/16 15:10:53 mike Exp $".
  198. //
  199.